package cn.dnui_zzh807.dnuifood_16110100807.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.dnui_zzh807.dnuifood_16110100807.R;
import cn.dnui_zzh807.dnuifood_16110100807.adapter.CollectAdapter;
import cn.dnui_zzh807.dnuifood_16110100807.adapter.FoodInfoAdapter;
import cn.dnui_zzh807.dnuifood_16110100807.bean.UserCollectBean;
import cn.dnui_zzh807.dnuifood_16110100807.listener.OnRetrofitListener;
import cn.dnui_zzh807.dnuifood_16110100807.model.CollectModel;

public class CollectShopFragment extends Fragment implements OnRetrofitListener<List<UserCollectBean>> {
    private UserCollectBean collectStatus;
    private RecyclerView recyclerView;
    private CollectAdapter adapter;
    private FoodInfoAdapter foodAdapter;
    private SharedPreferences sharedPreferences;
    public CollectShopFragment() {
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        sharedPreferences=view.getContext().getSharedPreferences("zzh", Context.MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");

        Toast.makeText(getActivity(),user_id,Toast.LENGTH_SHORT).show();
        recyclerView=view.findViewById(R.id.shop_collected_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //recyclerView.setAdapter(new ShopAdapter(getActivity(),getData()));
        initViews();
        initData(user_id);

    }

    void initViews(){
        recyclerView = recyclerView.findViewById(R.id.shop_collected_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    void initData( String user_id){
        CollectModel collectModel = new CollectModel();
        collectModel.getAllUserCollection(user_id,"0",this);  //访问服务器请求
    }

    @Override
    public void onSuccess(List<UserCollectBean> collectStatuses, int flag) {
        adapter = new CollectAdapter(getActivity(),collectStatuses);
        //foodAdapter = new FoodAdapter(getActivity(),collectStatuses);
        recyclerView.setAdapter(adapter);
       // Toast.makeText(getActivity(),"网络ok",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFail() {
        Toast.makeText(getActivity(),"网络错误",Toast.LENGTH_SHORT).show();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.activity_collect_shop_fragment, container, false);
    }

}
